/*
 * Created by JFormDesigner on Sat Dec 03 22:23:38 ICT 2011
 */

package realestatemarketclient;

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import java.util.List;
import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 * @author Tran Cong
 */
public class NumOfCustomerForm extends JFrame {
    public NumOfCustomerForm() {
        initComponents();
    }
    
    private void loadTable(String selected, int index){
        try{
                cusReal = RealEstateService.getRequest().getAllCusReal().getCUSTOMERREALESTATE();
                DefaultTableModel dtm = new DefaultTableModel();
                dtm.addColumn("STT");
                dtm.addColumn("Customers Name");
                dtm.addColumn("RealEstate Type Name");
                int i = 1;
                for (REMService.CUSTOMERREALESTATE cr : cusReal){
                    if(cr.getCustomerName().equals(selected) && index !=0){
                        Vector v = new Vector();
                        v.add(i++);
                        v.add(cr.getCustomerName());
                        v.add(cr.getRealEstateTypeName());
                        dtm.addRow(v);
                    }
                    if(index == 0){
                        Vector v = new Vector();
                        v.add(i++);
                        v.add(cr.getCustomerName());
                        v.add(cr.getRealEstateTypeName());
                        dtm.addRow(v);
                    }
                }
                customerTable.setModel(dtm);
                customerTable.getColumnModel().getColumn(0).setPreferredWidth(5);
            }
        catch(Exception e){
            JOptionPane.showMessageDialog(this, e.toString());
        }
    }
    
    private void loadCustomers(){
        customers = RealEstateService.getRequest().getAllCustomers().getCUSTOMER();
        for(REMService.CUSTOMER c : customers){
            customerComboBox.addItem(c.getName());
        }
    }
    
    private void buttonShowActionPerformed(ActionEvent e) {
        // TODO add your code here
        loadTable(customerComboBox.getSelectedItem().toString(), customerComboBox.getSelectedIndex());
    }

    private void thisWindowOpened(WindowEvent e) {
        // TODO add your code here
        loadCustomers();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - Tran Cong
        label1 = new JLabel();
        customerComboBox = new JComboBox();
        buttonShow = new JButton();
        scrollPane1 = new JScrollPane();
        customerTable = new JTable();

        //======== this ========
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowOpened(WindowEvent e) {
                thisWindowOpened(e);
            }
        });
        Container contentPane = getContentPane();
        contentPane.setLayout(new FormLayout(
            "47dlu, $lcgap, default, $lcgap, 40dlu, $lcgap, 120dlu, $lcgap, 49dlu, $lcgap, default, $lcgap, 51dlu",
            "fill:13dlu, 3*($lgap, default), $lgap, 111dlu, $lgap, 30dlu"));

        //---- label1 ----
        label1.setText("Kh\u00e1ch h\u00e0ng");
        contentPane.add(label1, CC.xy(5, 5));

        //---- customerComboBox ----
        customerComboBox.setModel(new DefaultComboBoxModel(new String[] {
            "All"
        }));
        contentPane.add(customerComboBox, CC.xy(7, 5));

        //---- buttonShow ----
        buttonShow.setText("OK");
        buttonShow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                buttonShowActionPerformed(e);
            }
        });
        contentPane.add(buttonShow, CC.xy(9, 5));

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(customerTable);
        }
        contentPane.add(scrollPane1, CC.xywh(3, 9, 10, 1));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - Tran Cong
    private JLabel label1;
    private JComboBox customerComboBox;
    private JButton buttonShow;
    private JScrollPane scrollPane1;
    private JTable customerTable;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
    
    private List<REMService.CUSTOMER> customers;
    private List<REMService.CUSTOMERREALESTATE> cusReal;
}
